package com.landi.carkey.ui;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.landi.carkey.MainActivity;
import com.landi.carkey.R;
import com.landi.carkey.core.BaseActivity;
import com.landi.carkey.util.DialogFactory;
import com.landi.carkey.util.StringUtil;
import com.landi.carkey.util.ToastUtil;

/**
 * @Description {登录页面}
 * @Creator wushan
 * @Date 2016/12/28 10:37
 */

public class LoginActivity extends BaseActivity {

    private EditText mUserNameEt;
    private EditText mPassWordEt;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            DialogFactory.dismissAlert(LoginActivity.this);
            switch (msg.what) {
                case 0://登录成功
                    ToastUtil.toast("登录成功");
                    goMainActivity();
                    break;

                case 211:
                    ToastUtil.toast("登录失败，找不到用户");
                    break;
                case 210:
                    ToastUtil.toast("登录失败，用户名和密码不匹配");
                    break;

                default:
                    ToastUtil.toast("登录失败，错误代码："+msg.what);
                    break;

            }
        }
    };

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_login, R.string.login, MODE_HOME);
    }


    private void goMainActivity() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void setUpView() {
        setToolbarRight("注册", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
            }
        });

        mUserNameEt = (EditText) findViewById(R.id.et_login_username);
        mPassWordEt = (EditText) findViewById(R.id.et_login_password);

    }

    @Override
    protected void setUpData() {

    }

    public void onMyButtonClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                String username = mUserNameEt.getText().toString().trim();
                String password = mPassWordEt.getText().toString().trim();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    ToastUtil.toast("用户名和密码不能为空");
                } else {
                    login(username, password);
                }
                break;
        }
    }

    private void login(String username, String password) {

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            ToastUtil.toast("用户名或密码不能为空");
            return;
        }

        DialogFactory.showLoadingDialog(this, "", "");


        if (StringUtil.isValidPhoneNo(username)) {//手机号密码登录
            AVUser.loginByMobilePhoneNumberInBackground(username, password, new LogInCallback<AVUser>() {
                @Override
                public void done(AVUser avUser, AVException e) {
                    if (e == null) {
                        if (avUser != null) {
                            handler.sendEmptyMessage(0);
                            Log.d("", "*****手机号密码登录***avuser*****" + avUser);
                        } else {
                            handler.sendEmptyMessage(1);
                        }

                    } else {
                        handler.sendEmptyMessage(e.getCode());
                        if (avUser != null) {
                            Log.d("", "***AVException**手机号密码登录***avuser*****" + avUser);
                        }
                    }
                }
            });

        } else {//用户名密码登录
            AVUser.logInInBackground(username, password, new LogInCallback<AVUser>() {
                @Override
                public void done(AVUser avUser, AVException e) {

                    if (e == null) {
                        if (avUser != null) {
                            handler.sendEmptyMessage(0);
                            Log.d("", "*****用户名密码登录***avuser*****" + avUser);
                        } else {
                            handler.sendEmptyMessage(1);
                        }

                    } else {
                        handler.sendEmptyMessage(e.getCode());
                        if (avUser != null) {
                            Log.d("", "***AVException**用户名密码登录***avuser*****" + avUser);
                        }
                    }
                }
            });
        }
    }
}
